/**
 * Copyright 2016 JustWayward Team
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ljfiction.book.tools;


import android.content.Context;

import com.ljfiction.book.Baner.ShuqianBean;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.model.HttpHeaders;

import java.util.List;
import java.util.UUID;

/**
 * @author yuyh.
 * @date 2016/9/23.
 */
public class SettingManager {

    private volatile static SettingManager manager;

    public static SettingManager getInstance() {
        return manager != null ? manager : (manager = new SettingManager());
    }

    public static void finishInstance() {
        if (manager != null) {
            manager = null;
        }
    }

    /*
     * 保存不是第一次进入阅读页
     * */
    public void saveFirst() {
        SharedPreferencesUtil.getInstance().putInt("isFirst", 1);
    }

    /*
     * 获取是否第一次进来，是就提示
     * */
    public int getFirst() {

        return SharedPreferencesUtil.getInstance().getInt("isFirst", 0);
    }

    /**
     * 保存书籍阅读字体大小
     *
     * @param bookId     需根据bookId对应，避免由于字体大小引起的分页不准确
     * @param fontSizePx
     * @return
     */
    public void saveFontSize(String bookId, int fontSizePx) {
        // 书籍对应
        SharedPreferencesUtil.getInstance().putInt(getFontSizeKey(bookId), fontSizePx);
    }

    /**
     * 保存全局生效的阅读字体大小
     *
     * @param fontSizePx
     */
    public void saveFontSize(int fontSizePx) {
        saveFontSize("", fontSizePx);
    }

    public int getReadFontSize(Context context, String bookId) {
        return SharedPreferencesUtil.getInstance().getInt(getFontSizeKey(bookId), getReadFontSize(context));
    }

    public int getReadFontSize(Context context) {
        return SharedPreferencesUtil.getInstance().getInt(getFontSizeKey(""), ScreenUtils.getNum(context, 18));
    }

    private String getFontSizeKey(String bookId) {
        return bookId + "-readFontSize";
    }

    public int getReadBrightness() {
        return ScreenUtils.getScreenBrightness();
    }

    /**
     * 保存阅读界面屏幕亮度
     *
     * @param percent 亮度比例 0~100
     */
    public void saveReadBrightness(int percent) {
        if (percent > 100) {
            ToastUtils.showToast("saveReadBrightnessErr CheckRefs");
            percent = 100;
        }
        SharedPreferencesUtil.getInstance().putInt(getLightnessKey(), percent);
    }

    private String getLightnessKey() {
        return "readLightness";
    }


    /*
     * 记录阅读位置
     * */

    public synchronized void saveReadProgress(String bookId, int currentChapter, int m_mbBufBeginPos, int m_mbBufEndPos, int m_mbpage, int m_mbbaocundiannum) {
        SharedPreferencesUtil.getInstance()
                .putInt(getChapterKey(bookId), currentChapter)
                .putInt(getStartPosKey(bookId), m_mbBufBeginPos)
                .putInt(getEndPosKey(bookId), m_mbBufEndPos)
                .putInt(getpageKey(bookId), m_mbpage)
                .putInt(getbaocundiannumKey(bookId), m_mbbaocundiannum);
    }

    /**
     * 获取上次阅读章节及位置
     *
     * @param bookId
     * @return
     */
    public int[] getReadProgress(String bookId) {
        int lastChapter = SharedPreferencesUtil.getInstance().getInt(getChapterKey(bookId), 0);
        int startPos = SharedPreferencesUtil.getInstance().getInt(getStartPosKey(bookId), 0);
        int endPos = SharedPreferencesUtil.getInstance().getInt(getEndPosKey(bookId), 0);
        int page = SharedPreferencesUtil.getInstance().getInt(getpageKey(bookId), 0);
        int baocundiannum = SharedPreferencesUtil.getInstance().getInt(getbaocundiannumKey(bookId), 0);

        return new int[]{lastChapter, startPos, endPos, page, baocundiannum};
    }

    public void removeReadProgress(String bookId) {
        SharedPreferencesUtil.getInstance()
                .remove(getChapterKey(bookId))
                .remove(getStartPosKey(bookId))
                .remove(getEndPosKey(bookId));
    }

    private String getChapterKey(String bookId) {
        return bookId + "-chapter";
    }

    private String getStartPosKey(String bookId) {
        return bookId + "-startPos";
    }

    private String getEndPosKey(String bookId) {
        return bookId + "-endPos";
    }

    private String getpageKey(String bookId) {
        return bookId + "-page";
    }

    private String getbaocundiannumKey(String bookId) {
        return bookId + "-baocundiannum";
    }

    /*
     * 获取背景颜色所属坐标
     * */
    public int getBeijingcolor() {
        return SharedPreferencesUtil.getInstance().getInt("beijingcolor", 0);
    }

    /*
     * 保存翻页方式
     * */
    public void saveflipwey(int position) {
        SharedPreferencesUtil.getInstance().putInt("flipwey", position);
    }

    /*
     * 获取翻页方式
     * */
    public int getflipwey() {
        return SharedPreferencesUtil.getInstance().getInt("flipwey", 1);
    }

    /*
     * 保存背景颜色所属坐标
     * */
    public void saveBeijingcolor(int position) {
        SharedPreferencesUtil.getInstance().putInt("beijingcolor", position);
    }

    /*
     * 记录阅读章节id
     * */
    public void saveBookChapterid(String bookId, int chapterid) {
        SharedPreferencesUtil.getInstance().putInt(bookId + "bookChapterId", chapterid);
    }

    public int getBookChapterid(String bookId) {
        return SharedPreferencesUtil.getInstance().getInt(bookId + "bookChapterId", 0);
    }

    public void saveBookVolumid(String bookId, int chapterid) {
        SharedPreferencesUtil.getInstance().putInt(bookId + "bookVolumid", chapterid);
    }

    public int getBookVolumid(String bookId) {
        return SharedPreferencesUtil.getInstance().getInt(bookId + "bookVolumid", 0);
    }

    public void saveReadTheme(int theme) {
        SharedPreferencesUtil.getInstance().putInt("readTheme", theme);
    }


    /**
     * 是否可以使用音量键翻页
     */
    public boolean isVolumeFlipEnable() {
        return SharedPreferencesUtil.getInstance().getBoolean("volumeFlip", true);
    }


    /*
     * 记录书签
     * */
    public void setShuqian(String bookid, List<ShuqianBean> baocunshuqian) {
        SharedPreferencesUtil.getInstance().putArray(bookid + "baocunshuqian", baocunshuqian);
    }

    /*
     * 获取书签
     * */
    public List<ShuqianBean> getShuqian(String bookid) {
        return SharedPreferencesUtil.getInstance().getArray(bookid + "baocunshuqian");
    }

    public void savesessionId(String sessionId) {
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.put("token",sessionId);
        OkHttpUtils.getInstance().addCommonHeaders(httpHeaders);
        SharedPreferencesUtil.getInstance().putString("sessionid", sessionId);
    }

    public String getSessionId() {
        return SharedPreferencesUtil.getInstance().getString("sessionid", "0");
    }



    public void putAuthorMessage(String kay, long volue) {
        SharedPreferencesUtil.getInstance().putlong(kay, volue);
    }

    public long getAuthorMessage(String kay) {
        return SharedPreferencesUtil.getInstance().getLong(kay, 0);
    }

    /*
     * 记录是否为夜间模式
     * */
    public void saveIsNightDay(int isNight) {
        SharedPreferencesUtil.getInstance().putInt("isNight", isNight);
    }

    public int getIsNightDay() {
        return SharedPreferencesUtil.getInstance().getInt("isNight", 0);
    }


    /*
     * 记录阅读页屏幕亮度
     * */
    public void saveSeekPosition(int seekPosition) {
        SharedPreferencesUtil.getInstance().putInt("seekPosition", seekPosition);
    }

    public int getSeekposition() {
        return SharedPreferencesUtil.getInstance().getInt("seekPosition", 0);
    }


    public void saveIsfirstPurse(int isfirstpurse) {
        SharedPreferencesUtil.getInstance().putInt("isfirstpurse", isfirstpurse);
    }

    public int getIsfirstPurse() {
        return SharedPreferencesUtil.getInstance().getInt("isfirstpurse", 0);
    }

    //记录最后查看读者应援的书籍id
    public void saveNewUpdateBookId(int bookId) {
        SharedPreferencesUtil.getInstance().putInt("newUpdateBookid", bookId);
    }

    public int getNewUpdateBookId() {
        return SharedPreferencesUtil.getInstance().getInt("newUpdateBookid", 0);
    }
    //记录作者更新的章节数
    public void saveNewChapterNum() {
        SharedPreferencesUtil.getInstance().putInt("NewChapterNum", SharedPreferencesUtil.getInstance().getInt("NewChapterNum", 0)+1);
    }

    public int getNewChapterNum() {
        return SharedPreferencesUtil.getInstance().getInt("NewChapterNum", 0);
    }
    //记录作者是否第一次打开应援页面
    public void savetIsFirstAid(int userId) {
        SharedPreferencesUtil.getInstance().setBoolean(String.valueOf(userId), false);
    }
    public boolean getIsFirstAid(int userId) {
        return SharedPreferencesUtil.getInstance().getBoolean(String.valueOf(userId), true);
    }
    public String getUUID(){
        String uuid=SharedPreferencesUtil.getInstance().getString("UUID","");
        if (uuid.equals("")||uuid==null){
            uuid=UUID.randomUUID().toString().replace("-","");
            SharedPreferencesUtil.getInstance().putString("UUID",uuid);
        }
      return   uuid;
    }


}
